package com.zero.jewelry.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zero.common.core.domain.page.PageDomain;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zero.common.annotation.log.Log;
import com.zero.common.core.basecontroller.BaseController;
import com.zero.common.core.domain.result.AjaxResult;
import com.zero.common.core.enums.BusinessType;
import com.zero.jewelry.domain.Category;
import com.zero.jewelry.service.ICategoryService;
import com.zero.common.core.utils.poi.ExcelUtil;
import com.zero.common.core.domain.page.TableDataInfo;

/**
 * 分类管理Controller
 *
 * @author cai-xu-kun
 * @date 2025-07-30
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "分类管理")
@RequestMapping("/jewelry/category")
public class CategoryController extends BaseController {

    private final ICategoryService categoryService;

    @ApiOperation("查询全部分类")
    @PreAuthorize("@ss.hasPermi('jewelry:category:list')")
    @GetMapping("/list/all")
    public AjaxResult listAll() {
//        根据id或者name查询
//        Category category = new Category();
//        category.setName("111");
        List<Category> list = categoryService.list();
        return success(list);
    }


//  ===========================================================

    /**
     * 查询分类管理分页
     */
    @ApiOperation("查询分类管理分页")
    @PreAuthorize("@ss.hasPermi('jewelry:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(Category category) {
        PageDomain pageDomain = this.startPage();
        Page<Category> p = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
        Page<Category> page = categoryService.selectCategoryListPage(category, p);
        return this.getDataTable(page);
    }

    /**
     * 导出分类管理列表
     */
    @ApiOperation("导出分类管理列表")
    @PreAuthorize("@ss.hasPermi('jewelry:category:export')")
    @Log(title = "分类管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Category category) {
        List<Category> list = categoryService.selectCategoryList(category);
        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
        util.exportExcel(response, list, "分类管理数据");
    }

    /**
     * 获取分类管理详细信息
     */
    @ApiOperation("获取分类管理详细信息")
    @PreAuthorize("@ss.hasPermi('jewelry:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(categoryService.selectCategoryById(id));
    }

    /**
     * 新增分类管理
     */
    @ApiOperation("新增分类管理")
    @PreAuthorize("@ss.hasPermi('jewelry:category:add')")
    @Log(title = "分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Category category) {
        return toAjax(categoryService.insertCategory(category));
    }

    /**
     * 修改分类管理
     */
    @ApiOperation("修改分类管理")
    @PreAuthorize("@ss.hasPermi('jewelry:category:edit')")
    @Log(title = "分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Category category) {
        return toAjax(categoryService.updateCategory(category));
    }

    /**
     * 删除分类管理
     */
    @ApiOperation("删除分类管理")
    @PreAuthorize("@ss.hasPermi('jewelry:category:remove')")
    @Log(title = "分类管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(categoryService.deleteCategoryByIds(ids));
    }
}
